library(cregg)
library(ggplot2)
library(ggpubr)
rm(list=ls())

#### Load data 

load("data.RData")

#### Formulas for models

f1 <- vote_choice ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula
f2 <- better_pol ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula
f3 <- prob_vote ~ age + prof+career + gender + legeff + voteror + voterknow + smedia ## Formula


### Run models by education levels

data3 <- data[is.na(data$ed_cat)==F,]

acmes.vote_choice_low <- cj(data3[data3$ed_cat=="Low" & data3$country!="Switzerland" & data3$country!="Germany",], f1, id = ~id)
acmes.better_pol_low <- cj(data3[data3$ed_cat=="Low" & data3$country!="Switzerland" & data3$country!="Germany",], f2, id = ~id)
acmes.prob_vote_low <- cj(data3[data3$ed_cat=="Low" & data3$country!="Switzerland" & data3$country!="Germany",], f3, id = ~id)

acmes.vote_choice_medium <- cj(data3[data3$ed_cat=="Medium" & data3$country!="Switzerland" & data3$country!="Germany",], f1, id = ~id)
acmes.better_pol_medium <- cj(data3[data3$ed_cat=="Medium" & data3$country!="Switzerland" & data3$country!="Germany",], f2, id = ~id)
acmes.prob_vote_medium <- cj(data3[data3$ed_cat=="Medium" & data3$country!="Switzerland" & data3$country!="Germany",], f3, id = ~id)


acmes.vote_choice_high <- cj(data3[data3$ed_cat=="High" & data3$country!="Switzerland" & data3$country!="Germany",], f1, id = ~id)
acmes.better_pol_high <- cj(data3[data3$ed_cat=="High" & data3$country!="Switzerland" & data3$country!="Germany",], f2, id = ~id)
acmes.prob_vote_high <- cj(data3[data3$ed_cat=="High" & data3$country!="Switzerland" & data3$country!="Germany",], f3, id = ~id)

mm.vote_choice_low <- mm(data3[data3$ed_cat=="Low" & data3$country!="Switzerland" & data3$country!="Germany",], f1, id = ~id)
mm.better_pol_low <- mm(data3[data3$ed_cat=="Low" & data3$country!="Switzerland" & data3$country!="Germany",], f2, id = ~id)
mm.prob_vote_low <- mm(data3[data3$ed_cat=="Low" & data3$country!="Switzerland" & data3$country!="Germany",], f3, id = ~id)

mm.vote_choice_medium <- mm(data3[data3$ed_cat=="Medium" & data3$country!="Switzerland" & data3$country!="Germany",], f1, id = ~id)
mm.better_pol_medium <- mm(data3[data3$ed_cat=="Medium" & data3$country!="Switzerland" & data3$country!="Germany",], f2, id = ~id)
mm.prob_vote_medium <- mm(data3[data3$ed_cat=="Medium" & data3$country!="Switzerland" & data3$country!="Germany",], f3, id = ~id)


mm.vote_choice_high <- mm(data3[data3$ed_cat=="High" & data3$country!="Switzerland" & data3$country!="Germany",], f1, id = ~id)
mm.better_pol_high <- mm(data3[data3$ed_cat=="High" & data3$country!="Switzerland" & data3$country!="Germany",], f2, id = ~id)
mm.prob_vote_high <- mm(data3[data3$ed_cat=="High" & data3$country!="Switzerland" & data3$country!="Germany",], f3, id = ~id)


acmes.vote_choice_low$ed_cat = "Low"
acmes.better_pol_low$ed_cat = "Low"
acmes.prob_vote_low$ed_cat = "Low"


acmes.vote_choice_medium$ed_cat = "Medium"
acmes.better_pol_medium$ed_cat = "Medium"
acmes.prob_vote_medium$ed_cat = "Medium"

acmes.vote_choice_high$ed_cat = "High"
acmes.better_pol_high$ed_cat = "High"
acmes.prob_vote_high$ed_cat = "High"



mm.vote_choice_low$ed_cat = "Low"
mm.better_pol_low$ed_cat = "Low"
mm.prob_vote_low$ed_cat = "Low"


mm.vote_choice_medium$ed_cat = "Medium"
mm.better_pol_medium$ed_cat = "Medium"
mm.prob_vote_medium$ed_cat = "Medium"

mm.vote_choice_high$ed_cat = "High"
mm.better_pol_high$ed_cat = "High"
mm.prob_vote_high$ed_cat = "High"


acmes.vote_choice_ed <- rbind(acmes.vote_choice_low, acmes.vote_choice_medium, acmes.vote_choice_high)
mm.vote_choice_ed <- rbind(mm.vote_choice_low, mm.vote_choice_medium, mm.vote_choice_high)

### Create data Figure A15

education_data_acmes.vote_choice <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                           "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                           "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                           "Gender", "Man", "Woman",
                                                           "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                           "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                           "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                           "Social Media", "Communicates regularly", "Does not communicate often"),3),
                                            labels_score = rep(c(13:30,1:12), 3),
                                            estimate = c(NA, acmes.vote_choice_ed$estimate[1:5],
                                                         NA, acmes.vote_choice_ed$estimate[6:10],
                                                         NA, acmes.vote_choice_ed$estimate[11:12],
                                                         NA, acmes.vote_choice_ed$estimate[13:14],
                                                         NA, acmes.vote_choice_ed$estimate[15:16],
                                                         NA, acmes.vote_choice_ed$estimate[17:18],
                                                         NA, acmes.vote_choice_ed$estimate[19:20],
                                                         NA, acmes.vote_choice_ed$estimate[21:22],
                                                         NA, acmes.vote_choice_ed$estimate[22+1:5],
                                                         NA, acmes.vote_choice_ed$estimate[22+6:10],
                                                         NA, acmes.vote_choice_ed$estimate[22+11:12],
                                                         NA, acmes.vote_choice_ed$estimate[22+13:14],
                                                         NA, acmes.vote_choice_ed$estimate[22+15:16],
                                                         NA, acmes.vote_choice_ed$estimate[22+17:18],
                                                         NA, acmes.vote_choice_ed$estimate[22+19:20],
                                                         NA, acmes.vote_choice_ed$estimate[22+21:22],
                                                         NA, acmes.vote_choice_ed$estimate[44+1:5],
                                                         NA, acmes.vote_choice_ed$estimate[44+6:10],
                                                         NA, acmes.vote_choice_ed$estimate[44+11:12],
                                                         NA, acmes.vote_choice_ed$estimate[44+13:14],
                                                         NA, acmes.vote_choice_ed$estimate[44+15:16],
                                                         NA, acmes.vote_choice_ed$estimate[44+17:18],
                                                         NA, acmes.vote_choice_ed$estimate[44+19:20],
                                                         NA, acmes.vote_choice_ed$estimate[44+21:22]),
                                            
                                            lower = c(NA, NA, acmes.vote_choice_ed$lower[2:5],
                                                      NA, NA, acmes.vote_choice_ed$lower[7:10],
                                                      NA, NA, acmes.vote_choice_ed$lower[12],
                                                      NA, NA, acmes.vote_choice_ed$lower[14],
                                                      NA, NA, acmes.vote_choice_ed$lower[16],
                                                      NA, NA, acmes.vote_choice_ed$lower[18],
                                                      NA, NA, acmes.vote_choice_ed$lower[20],
                                                      NA, NA, acmes.vote_choice_ed$lower[22],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+2:5],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+7:10],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+12],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+14],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+16],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+18],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+20],
                                                      NA, NA, acmes.vote_choice_ed$lower[22+22],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+2:5],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+7:10],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+12],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+14],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+16],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+18],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+20],
                                                      NA, NA, acmes.vote_choice_ed$lower[44+22]),
                                            upper = c(NA, NA, acmes.vote_choice_ed$upper[2:5],
                                                      NA, NA, acmes.vote_choice_ed$upper[7:10],
                                                      NA, NA, acmes.vote_choice_ed$upper[12],
                                                      NA, NA, acmes.vote_choice_ed$upper[14],
                                                      NA, NA, acmes.vote_choice_ed$upper[16],
                                                      NA, NA, acmes.vote_choice_ed$upper[18],
                                                      NA, NA, acmes.vote_choice_ed$upper[20],
                                                      NA, NA, acmes.vote_choice_ed$upper[22],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+2:5],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+7:10],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+12],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+14],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+16],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+18],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+20],
                                                      NA, NA, acmes.vote_choice_ed$upper[22+22],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+2:5],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+7:10],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+12],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+14],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+16],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+18],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+20],
                                                      NA, NA, acmes.vote_choice_ed$upper[44+22]),
                                            Education = rep(c("Low", "Medium", "High"), each=30),
                                            Education_score = rep(c(0, 2, 4), each=30)))



education_data_mm.vote_choice <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                        "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                        "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                        "Gender", "Man", "Woman",
                                                        "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                        "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                        "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                        "Social Media", "Communicates regularly", "Does not communicate often"),3),
                                         labels_score = rep(c(13:30,1:12), 3),
                                         estimate = c(NA, mm.vote_choice_ed$estimate[1:5],
                                                      NA, mm.vote_choice_ed$estimate[6:10],
                                                      NA, mm.vote_choice_ed$estimate[11:12],
                                                      NA, mm.vote_choice_ed$estimate[13:14],
                                                      NA, mm.vote_choice_ed$estimate[15:16],
                                                      NA, mm.vote_choice_ed$estimate[17:18],
                                                      NA, mm.vote_choice_ed$estimate[19:20],
                                                      NA, mm.vote_choice_ed$estimate[21:22],
                                                      NA, mm.vote_choice_ed$estimate[22+1:5],
                                                      NA, mm.vote_choice_ed$estimate[22+6:10],
                                                      NA, mm.vote_choice_ed$estimate[22+11:12],
                                                      NA, mm.vote_choice_ed$estimate[22+13:14],
                                                      NA, mm.vote_choice_ed$estimate[22+15:16],
                                                      NA, mm.vote_choice_ed$estimate[22+17:18],
                                                      NA, mm.vote_choice_ed$estimate[22+19:20],
                                                      NA, mm.vote_choice_ed$estimate[22+21:22],
                                                      NA, mm.vote_choice_ed$estimate[44+1:5],
                                                      NA, mm.vote_choice_ed$estimate[44+6:10],
                                                      NA, mm.vote_choice_ed$estimate[44+11:12],
                                                      NA, mm.vote_choice_ed$estimate[44+13:14],
                                                      NA, mm.vote_choice_ed$estimate[44+15:16],
                                                      NA, mm.vote_choice_ed$estimate[44+17:18],
                                                      NA, mm.vote_choice_ed$estimate[44+19:20],
                                                      NA, mm.vote_choice_ed$estimate[44+21:22]),
                                         
                                         lower = c(NA, mm.vote_choice_ed$lower[1:5],
                                                   NA, mm.vote_choice_ed$lower[6:10],
                                                   NA, mm.vote_choice_ed$lower[11:12],
                                                   NA, mm.vote_choice_ed$lower[13:14],
                                                   NA, mm.vote_choice_ed$lower[15:16],
                                                   NA, mm.vote_choice_ed$lower[17:18],
                                                   NA, mm.vote_choice_ed$lower[19:20],
                                                   NA, mm.vote_choice_ed$lower[21:22],
                                                   NA, mm.vote_choice_ed$lower[22+1:5],
                                                   NA, mm.vote_choice_ed$lower[22+6:10],
                                                   NA, mm.vote_choice_ed$lower[22+11:12],
                                                   NA, mm.vote_choice_ed$lower[22+13:14],
                                                   NA, mm.vote_choice_ed$lower[22+15:16],
                                                   NA, mm.vote_choice_ed$lower[22+17:18],
                                                   NA, mm.vote_choice_ed$lower[22+19:20],
                                                   NA, mm.vote_choice_ed$lower[22+21:22],
                                                   NA, mm.vote_choice_ed$lower[44+1:5],
                                                   NA, mm.vote_choice_ed$lower[44+6:10],
                                                   NA, mm.vote_choice_ed$lower[44+11:12],
                                                   NA, mm.vote_choice_ed$lower[44+13:14],
                                                   NA, mm.vote_choice_ed$lower[44+15:16],
                                                   NA, mm.vote_choice_ed$lower[44+17:18],
                                                   NA, mm.vote_choice_ed$lower[44+19:20],
                                                   NA, mm.vote_choice_ed$lower[44+21:22]),
                                         upper = c(NA, mm.vote_choice_ed$upper[1:5],
                                                   NA, mm.vote_choice_ed$upper[6:10],
                                                   NA, mm.vote_choice_ed$upper[11:12],
                                                   NA, mm.vote_choice_ed$upper[13:14],
                                                   NA, mm.vote_choice_ed$upper[15:16],
                                                   NA, mm.vote_choice_ed$upper[17:18],
                                                   NA, mm.vote_choice_ed$upper[19:20],
                                                   NA, mm.vote_choice_ed$upper[21:22],
                                                   NA, mm.vote_choice_ed$upper[22+1:5],
                                                   NA, mm.vote_choice_ed$upper[22+6:10],
                                                   NA, mm.vote_choice_ed$upper[22+11:12],
                                                   NA, mm.vote_choice_ed$upper[22+13:14],
                                                   NA, mm.vote_choice_ed$upper[22+15:16],
                                                   NA, mm.vote_choice_ed$upper[22+17:18],
                                                   NA, mm.vote_choice_ed$upper[22+19:20],
                                                   NA, mm.vote_choice_ed$upper[22+21:22],
                                                   NA, mm.vote_choice_ed$upper[44+1:5],
                                                   NA, mm.vote_choice_ed$upper[44+6:10],
                                                   NA, mm.vote_choice_ed$upper[44+11:12],
                                                   NA, mm.vote_choice_ed$upper[44+13:14],
                                                   NA, mm.vote_choice_ed$upper[44+15:16],
                                                   NA, mm.vote_choice_ed$upper[44+17:18],
                                                   NA, mm.vote_choice_ed$upper[44+19:20],
                                                   NA, mm.vote_choice_ed$upper[44+21:22]),
                                         Education = rep(c("Low", "Medium", "High"), each=30),
                                         Education_score = rep(c(0, 2, 4), each=30)))


education_data_acmes.vote_choice$type <- "Acmes"
education_data_mm.vote_choice$type <- "Marginal Means"


education_data_acmes.vote_choice$labels_score2 <- as.numeric(paste0(education_data_acmes.vote_choice$labels_score, ".", education_data_acmes.vote_choice$Education_score))
education_data_mm.vote_choice$labels_score2 <- as.numeric(paste0(education_data_mm.vote_choice$labels_score, ".", education_data_mm.vote_choice$Education_score))

### Create data Figure A16

acmes.better_pol_ed <- rbind(acmes.better_pol_low, acmes.better_pol_medium, acmes.better_pol_high)
mm.better_pol_ed <- rbind(mm.better_pol_low, mm.better_pol_medium, mm.better_pol_high)

education_data_acmes.better_pol <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                                     "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                     "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                     "Gender", "Man", "Woman",
                                                                     "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                                     "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                                     "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                                     "Social Media", "Communicates regularly", "Does not communicate often"),3),
                                                      labels_score = rep(c(13:30,1:12), 3),
                                                      estimate = c(NA, acmes.better_pol_ed$estimate[1:5],
                                                                   NA, acmes.better_pol_ed$estimate[6:10],
                                                                   NA, acmes.better_pol_ed$estimate[11:12],
                                                                   NA, acmes.better_pol_ed$estimate[13:14],
                                                                   NA, acmes.better_pol_ed$estimate[15:16],
                                                                   NA, acmes.better_pol_ed$estimate[17:18],
                                                                   NA, acmes.better_pol_ed$estimate[19:20],
                                                                   NA, acmes.better_pol_ed$estimate[21:22],
                                                                   NA, acmes.better_pol_ed$estimate[22+1:5],
                                                                   NA, acmes.better_pol_ed$estimate[22+6:10],
                                                                   NA, acmes.better_pol_ed$estimate[22+11:12],
                                                                   NA, acmes.better_pol_ed$estimate[22+13:14],
                                                                   NA, acmes.better_pol_ed$estimate[22+15:16],
                                                                   NA, acmes.better_pol_ed$estimate[22+17:18],
                                                                   NA, acmes.better_pol_ed$estimate[22+19:20],
                                                                   NA, acmes.better_pol_ed$estimate[22+21:22],
                                                                   NA, acmes.better_pol_ed$estimate[44+1:5],
                                                                   NA, acmes.better_pol_ed$estimate[44+6:10],
                                                                   NA, acmes.better_pol_ed$estimate[44+11:12],
                                                                   NA, acmes.better_pol_ed$estimate[44+13:14],
                                                                   NA, acmes.better_pol_ed$estimate[44+15:16],
                                                                   NA, acmes.better_pol_ed$estimate[44+17:18],
                                                                   NA, acmes.better_pol_ed$estimate[44+19:20],
                                                                   NA, acmes.better_pol_ed$estimate[44+21:22]),
                                                      
                                                      lower = c(NA, NA, acmes.better_pol_ed$lower[2:5],
                                                                NA, NA, acmes.better_pol_ed$lower[7:10],
                                                                NA, NA, acmes.better_pol_ed$lower[12],
                                                                NA, NA, acmes.better_pol_ed$lower[14],
                                                                NA, NA, acmes.better_pol_ed$lower[16],
                                                                NA, NA, acmes.better_pol_ed$lower[18],
                                                                NA, NA, acmes.better_pol_ed$lower[20],
                                                                NA, NA, acmes.better_pol_ed$lower[22],
                                                                NA, NA, acmes.better_pol_ed$lower[22+2:5],
                                                                NA, NA, acmes.better_pol_ed$lower[22+7:10],
                                                                NA, NA, acmes.better_pol_ed$lower[22+12],
                                                                NA, NA, acmes.better_pol_ed$lower[22+14],
                                                                NA, NA, acmes.better_pol_ed$lower[22+16],
                                                                NA, NA, acmes.better_pol_ed$lower[22+18],
                                                                NA, NA, acmes.better_pol_ed$lower[22+20],
                                                                NA, NA, acmes.better_pol_ed$lower[22+22],
                                                                NA, NA, acmes.better_pol_ed$lower[44+2:5],
                                                                NA, NA, acmes.better_pol_ed$lower[44+7:10],
                                                                NA, NA, acmes.better_pol_ed$lower[44+12],
                                                                NA, NA, acmes.better_pol_ed$lower[44+14],
                                                                NA, NA, acmes.better_pol_ed$lower[44+16],
                                                                NA, NA, acmes.better_pol_ed$lower[44+18],
                                                                NA, NA, acmes.better_pol_ed$lower[44+20],
                                                                NA, NA, acmes.better_pol_ed$lower[44+22]),
                                                      upper = c(NA, NA, acmes.better_pol_ed$upper[2:5],
                                                                NA, NA, acmes.better_pol_ed$upper[7:10],
                                                                NA, NA, acmes.better_pol_ed$upper[12],
                                                                NA, NA, acmes.better_pol_ed$upper[14],
                                                                NA, NA, acmes.better_pol_ed$upper[16],
                                                                NA, NA, acmes.better_pol_ed$upper[18],
                                                                NA, NA, acmes.better_pol_ed$upper[20],
                                                                NA, NA, acmes.better_pol_ed$upper[22],
                                                                NA, NA, acmes.better_pol_ed$upper[22+2:5],
                                                                NA, NA, acmes.better_pol_ed$upper[22+7:10],
                                                                NA, NA, acmes.better_pol_ed$upper[22+12],
                                                                NA, NA, acmes.better_pol_ed$upper[22+14],
                                                                NA, NA, acmes.better_pol_ed$upper[22+16],
                                                                NA, NA, acmes.better_pol_ed$upper[22+18],
                                                                NA, NA, acmes.better_pol_ed$upper[22+20],
                                                                NA, NA, acmes.better_pol_ed$upper[22+22],
                                                                NA, NA, acmes.better_pol_ed$upper[44+2:5],
                                                                NA, NA, acmes.better_pol_ed$upper[44+7:10],
                                                                NA, NA, acmes.better_pol_ed$upper[44+12],
                                                                NA, NA, acmes.better_pol_ed$upper[44+14],
                                                                NA, NA, acmes.better_pol_ed$upper[44+16],
                                                                NA, NA, acmes.better_pol_ed$upper[44+18],
                                                                NA, NA, acmes.better_pol_ed$upper[44+20],
                                                                NA, NA, acmes.better_pol_ed$upper[44+22]),
                                                      Education = rep(c("Low", "Medium", "High"), each=30),
                                                      Education_score = rep(c(0, 2, 4), each=30)))


education_data_mm.better_pol <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                                  "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                  "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                  "Gender", "Man", "Woman",
                                                                  "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                                  "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                                  "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                                  "Social Media", "Communicates regularly", "Does not communicate often"),3),
                                                   labels_score = rep(c(13:30,1:12), 3),
                                                   estimate = c(NA, mm.better_pol_ed$estimate[1:5],
                                                                NA, mm.better_pol_ed$estimate[6:10],
                                                                NA, mm.better_pol_ed$estimate[11:12],
                                                                NA, mm.better_pol_ed$estimate[13:14],
                                                                NA, mm.better_pol_ed$estimate[15:16],
                                                                NA, mm.better_pol_ed$estimate[17:18],
                                                                NA, mm.better_pol_ed$estimate[19:20],
                                                                NA, mm.better_pol_ed$estimate[21:22],
                                                                NA, mm.better_pol_ed$estimate[22+1:5],
                                                                NA, mm.better_pol_ed$estimate[22+6:10],
                                                                NA, mm.better_pol_ed$estimate[22+11:12],
                                                                NA, mm.better_pol_ed$estimate[22+13:14],
                                                                NA, mm.better_pol_ed$estimate[22+15:16],
                                                                NA, mm.better_pol_ed$estimate[22+17:18],
                                                                NA, mm.better_pol_ed$estimate[22+19:20],
                                                                NA, mm.better_pol_ed$estimate[22+21:22],
                                                                NA, mm.better_pol_ed$estimate[44+1:5],
                                                                NA, mm.better_pol_ed$estimate[44+6:10],
                                                                NA, mm.better_pol_ed$estimate[44+11:12],
                                                                NA, mm.better_pol_ed$estimate[44+13:14],
                                                                NA, mm.better_pol_ed$estimate[44+15:16],
                                                                NA, mm.better_pol_ed$estimate[44+17:18],
                                                                NA, mm.better_pol_ed$estimate[44+19:20],
                                                                NA, mm.better_pol_ed$estimate[44+21:22]),
                                                   
                                                   lower = c(NA, mm.better_pol_ed$lower[1:5],
                                                             NA, mm.better_pol_ed$lower[6:10],
                                                             NA, mm.better_pol_ed$lower[11:12],
                                                             NA, mm.better_pol_ed$lower[13:14],
                                                             NA, mm.better_pol_ed$lower[15:16],
                                                             NA, mm.better_pol_ed$lower[17:18],
                                                             NA, mm.better_pol_ed$lower[19:20],
                                                             NA, mm.better_pol_ed$lower[21:22],
                                                             NA, mm.better_pol_ed$lower[22+1:5],
                                                             NA, mm.better_pol_ed$lower[22+6:10],
                                                             NA, mm.better_pol_ed$lower[22+11:12],
                                                             NA, mm.better_pol_ed$lower[22+13:14],
                                                             NA, mm.better_pol_ed$lower[22+15:16],
                                                             NA, mm.better_pol_ed$lower[22+17:18],
                                                             NA, mm.better_pol_ed$lower[22+19:20],
                                                             NA, mm.better_pol_ed$lower[22+21:22],
                                                             NA, mm.better_pol_ed$lower[44+1:5],
                                                             NA, mm.better_pol_ed$lower[44+6:10],
                                                             NA, mm.better_pol_ed$lower[44+11:12],
                                                             NA, mm.better_pol_ed$lower[44+13:14],
                                                             NA, mm.better_pol_ed$lower[44+15:16],
                                                             NA, mm.better_pol_ed$lower[44+17:18],
                                                             NA, mm.better_pol_ed$lower[44+19:20],
                                                             NA, mm.better_pol_ed$lower[44+21:22]),
                                                   upper = c(NA, mm.better_pol_ed$upper[1:5],
                                                             NA, mm.better_pol_ed$upper[6:10],
                                                             NA, mm.better_pol_ed$upper[11:12],
                                                             NA, mm.better_pol_ed$upper[13:14],
                                                             NA, mm.better_pol_ed$upper[15:16],
                                                             NA, mm.better_pol_ed$upper[17:18],
                                                             NA, mm.better_pol_ed$upper[19:20],
                                                             NA, mm.better_pol_ed$upper[21:22],
                                                             NA, mm.better_pol_ed$upper[22+1:5],
                                                             NA, mm.better_pol_ed$upper[22+6:10],
                                                             NA, mm.better_pol_ed$upper[22+11:12],
                                                             NA, mm.better_pol_ed$upper[22+13:14],
                                                             NA, mm.better_pol_ed$upper[22+15:16],
                                                             NA, mm.better_pol_ed$upper[22+17:18],
                                                             NA, mm.better_pol_ed$upper[22+19:20],
                                                             NA, mm.better_pol_ed$upper[22+21:22],
                                                             NA, mm.better_pol_ed$upper[44+1:5],
                                                             NA, mm.better_pol_ed$upper[44+6:10],
                                                             NA, mm.better_pol_ed$upper[44+11:12],
                                                             NA, mm.better_pol_ed$upper[44+13:14],
                                                             NA, mm.better_pol_ed$upper[44+15:16],
                                                             NA, mm.better_pol_ed$upper[44+17:18],
                                                             NA, mm.better_pol_ed$upper[44+19:20],
                                                             NA, mm.better_pol_ed$upper[44+21:22]),
                                                   Education = rep(c("Low", "Medium", "High"), each=30),
                                                   Education_score = rep(c(0, 2, 4), each=30)))

education_data_acmes.better_pol$type <- "Acmes"
education_data_mm.better_pol$type <- "Marginal Means"


education_data_acmes.better_pol$labels_score2 <- as.numeric(paste0(education_data_acmes.better_pol$labels_score, ".", education_data_acmes.better_pol$Education_score))
education_data_mm.better_pol$labels_score2 <- as.numeric(paste0(education_data_mm.better_pol$labels_score, ".", education_data_mm.better_pol$Education_score))

### Create data Figure A17


acmes.prob_vote_ed <- rbind(acmes.prob_vote_low, acmes.prob_vote_medium, acmes.prob_vote_high)
mm.prob_vote_ed <- rbind(mm.prob_vote_low, mm.prob_vote_medium, mm.prob_vote_high)

education_data_acmes.prob_vote <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                                    "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                    "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                    "Gender", "Man", "Woman",
                                                                    "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                                    "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                                    "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                                    "Social Media", "Communicates regularly", "Does not communicate often"),3),
                                                     labels_score = rep(c(13:30,1:12), 3),
                                                     estimate = c(NA, acmes.prob_vote_ed$estimate[1:5],
                                                                  NA, acmes.prob_vote_ed$estimate[6:10],
                                                                  NA, acmes.prob_vote_ed$estimate[11:12],
                                                                  NA, acmes.prob_vote_ed$estimate[13:14],
                                                                  NA, acmes.prob_vote_ed$estimate[15:16],
                                                                  NA, acmes.prob_vote_ed$estimate[17:18],
                                                                  NA, acmes.prob_vote_ed$estimate[19:20],
                                                                  NA, acmes.prob_vote_ed$estimate[21:22],
                                                                  NA, acmes.prob_vote_ed$estimate[22+1:5],
                                                                  NA, acmes.prob_vote_ed$estimate[22+6:10],
                                                                  NA, acmes.prob_vote_ed$estimate[22+11:12],
                                                                  NA, acmes.prob_vote_ed$estimate[22+13:14],
                                                                  NA, acmes.prob_vote_ed$estimate[22+15:16],
                                                                  NA, acmes.prob_vote_ed$estimate[22+17:18],
                                                                  NA, acmes.prob_vote_ed$estimate[22+19:20],
                                                                  NA, acmes.prob_vote_ed$estimate[22+21:22],
                                                                  NA, acmes.prob_vote_ed$estimate[44+1:5],
                                                                  NA, acmes.prob_vote_ed$estimate[44+6:10],
                                                                  NA, acmes.prob_vote_ed$estimate[44+11:12],
                                                                  NA, acmes.prob_vote_ed$estimate[44+13:14],
                                                                  NA, acmes.prob_vote_ed$estimate[44+15:16],
                                                                  NA, acmes.prob_vote_ed$estimate[44+17:18],
                                                                  NA, acmes.prob_vote_ed$estimate[44+19:20],
                                                                  NA, acmes.prob_vote_ed$estimate[44+21:22]),
                                                     
                                                     lower = c(NA, NA, acmes.prob_vote_ed$lower[2:5],
                                                               NA, NA, acmes.prob_vote_ed$lower[7:10],
                                                               NA, NA, acmes.prob_vote_ed$lower[12],
                                                               NA, NA, acmes.prob_vote_ed$lower[14],
                                                               NA, NA, acmes.prob_vote_ed$lower[16],
                                                               NA, NA, acmes.prob_vote_ed$lower[18],
                                                               NA, NA, acmes.prob_vote_ed$lower[20],
                                                               NA, NA, acmes.prob_vote_ed$lower[22],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+2:5],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+7:10],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+12],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+14],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+16],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+18],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+20],
                                                               NA, NA, acmes.prob_vote_ed$lower[22+22],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+2:5],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+7:10],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+12],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+14],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+16],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+18],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+20],
                                                               NA, NA, acmes.prob_vote_ed$lower[44+22]),
                                                     upper = c(NA, NA, acmes.prob_vote_ed$upper[2:5],
                                                               NA, NA, acmes.prob_vote_ed$upper[7:10],
                                                               NA, NA, acmes.prob_vote_ed$upper[12],
                                                               NA, NA, acmes.prob_vote_ed$upper[14],
                                                               NA, NA, acmes.prob_vote_ed$upper[16],
                                                               NA, NA, acmes.prob_vote_ed$upper[18],
                                                               NA, NA, acmes.prob_vote_ed$upper[20],
                                                               NA, NA, acmes.prob_vote_ed$upper[22],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+2:5],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+7:10],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+12],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+14],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+16],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+18],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+20],
                                                               NA, NA, acmes.prob_vote_ed$upper[22+22],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+2:5],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+7:10],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+12],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+14],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+16],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+18],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+20],
                                                               NA, NA, acmes.prob_vote_ed$upper[44+22]),
                                                     Education = rep(c("Low", "Medium", "High"), each=30),
                                                     Education_score = rep(c(0, 2, 4), each=30)))


education_data_mm.prob_vote <- as.data.frame(list(labels = rep(c("Age", "33", "43", "53", "63", "73",
                                                                 "Profession", "Buisness Person", "Farmer", "Lawyer", "Medical doctor", "Parliamentary assistant",
                                                                 "Career", "Joined party 3 years ago", "Party offices in last 15 years",
                                                                 "Gender", "Man", "Woman",
                                                                 "Legislative Effectiveness", "Successful in parliament", "Not very successful in parliament",
                                                                 "Presence in Constituency", "Rarely in constituency", "Often in constituency",
                                                                 "Knowledge of voters", "Does not know voters well", "Knows voters well",
                                                                 "Social Media", "Communicates regularly", "Does not communicate often"),3),
                                                  labels_score = rep(c(13:30,1:12), 3),
                                                  estimate = c(NA, mm.prob_vote_ed$estimate[1:5],
                                                               NA, mm.prob_vote_ed$estimate[6:10],
                                                               NA, mm.prob_vote_ed$estimate[11:12],
                                                               NA, mm.prob_vote_ed$estimate[13:14],
                                                               NA, mm.prob_vote_ed$estimate[15:16],
                                                               NA, mm.prob_vote_ed$estimate[17:18],
                                                               NA, mm.prob_vote_ed$estimate[19:20],
                                                               NA, mm.prob_vote_ed$estimate[21:22],
                                                               NA, mm.prob_vote_ed$estimate[22+1:5],
                                                               NA, mm.prob_vote_ed$estimate[22+6:10],
                                                               NA, mm.prob_vote_ed$estimate[22+11:12],
                                                               NA, mm.prob_vote_ed$estimate[22+13:14],
                                                               NA, mm.prob_vote_ed$estimate[22+15:16],
                                                               NA, mm.prob_vote_ed$estimate[22+17:18],
                                                               NA, mm.prob_vote_ed$estimate[22+19:20],
                                                               NA, mm.prob_vote_ed$estimate[22+21:22],
                                                               NA, mm.prob_vote_ed$estimate[44+1:5],
                                                               NA, mm.prob_vote_ed$estimate[44+6:10],
                                                               NA, mm.prob_vote_ed$estimate[44+11:12],
                                                               NA, mm.prob_vote_ed$estimate[44+13:14],
                                                               NA, mm.prob_vote_ed$estimate[44+15:16],
                                                               NA, mm.prob_vote_ed$estimate[44+17:18],
                                                               NA, mm.prob_vote_ed$estimate[44+19:20],
                                                               NA, mm.prob_vote_ed$estimate[44+21:22]),
                                                  
                                                  lower = c(NA, mm.prob_vote_ed$lower[1:5],
                                                            NA, mm.prob_vote_ed$lower[6:10],
                                                            NA, mm.prob_vote_ed$lower[11:12],
                                                            NA, mm.prob_vote_ed$lower[13:14],
                                                            NA, mm.prob_vote_ed$lower[15:16],
                                                            NA, mm.prob_vote_ed$lower[17:18],
                                                            NA, mm.prob_vote_ed$lower[19:20],
                                                            NA, mm.prob_vote_ed$lower[21:22],
                                                            NA, mm.prob_vote_ed$lower[22+1:5],
                                                            NA, mm.prob_vote_ed$lower[22+6:10],
                                                            NA, mm.prob_vote_ed$lower[22+11:12],
                                                            NA, mm.prob_vote_ed$lower[22+13:14],
                                                            NA, mm.prob_vote_ed$lower[22+15:16],
                                                            NA, mm.prob_vote_ed$lower[22+17:18],
                                                            NA, mm.prob_vote_ed$lower[22+19:20],
                                                            NA, mm.prob_vote_ed$lower[22+21:22],
                                                            NA, mm.prob_vote_ed$lower[44+1:5],
                                                            NA, mm.prob_vote_ed$lower[44+6:10],
                                                            NA, mm.prob_vote_ed$lower[44+11:12],
                                                            NA, mm.prob_vote_ed$lower[44+13:14],
                                                            NA, mm.prob_vote_ed$lower[44+15:16],
                                                            NA, mm.prob_vote_ed$lower[44+17:18],
                                                            NA, mm.prob_vote_ed$lower[44+19:20],
                                                            NA, mm.prob_vote_ed$lower[44+21:22]),
                                                  upper = c(NA, mm.prob_vote_ed$upper[1:5],
                                                            NA, mm.prob_vote_ed$upper[6:10],
                                                            NA, mm.prob_vote_ed$upper[11:12],
                                                            NA, mm.prob_vote_ed$upper[13:14],
                                                            NA, mm.prob_vote_ed$upper[15:16],
                                                            NA, mm.prob_vote_ed$upper[17:18],
                                                            NA, mm.prob_vote_ed$upper[19:20],
                                                            NA, mm.prob_vote_ed$upper[21:22],
                                                            NA, mm.prob_vote_ed$upper[22+1:5],
                                                            NA, mm.prob_vote_ed$upper[22+6:10],
                                                            NA, mm.prob_vote_ed$upper[22+11:12],
                                                            NA, mm.prob_vote_ed$upper[22+13:14],
                                                            NA, mm.prob_vote_ed$upper[22+15:16],
                                                            NA, mm.prob_vote_ed$upper[22+17:18],
                                                            NA, mm.prob_vote_ed$upper[22+19:20],
                                                            NA, mm.prob_vote_ed$upper[22+21:22],
                                                            NA, mm.prob_vote_ed$upper[44+1:5],
                                                            NA, mm.prob_vote_ed$upper[44+6:10],
                                                            NA, mm.prob_vote_ed$upper[44+11:12],
                                                            NA, mm.prob_vote_ed$upper[44+13:14],
                                                            NA, mm.prob_vote_ed$upper[44+15:16],
                                                            NA, mm.prob_vote_ed$upper[44+17:18],
                                                            NA, mm.prob_vote_ed$upper[44+19:20],
                                                            NA, mm.prob_vote_ed$upper[44+21:22]),
                                                  Education = rep(c("Low", "Medium", "High"), each=30),
                                                  Education_score = rep(c(0, 2, 4), each=30)))


education_data_acmes.prob_vote$type <- "Acmes"
education_data_mm.prob_vote$type <- "Marginal Means"


education_data_acmes.prob_vote$labels_score2 <- as.numeric(paste0(education_data_acmes.prob_vote$labels_score, ".", education_data_acmes.prob_vote$Education_score))
education_data_mm.prob_vote$labels_score2 <- as.numeric(paste0(education_data_mm.prob_vote$labels_score, ".", education_data_mm.prob_vote$Education_score))


## Plot Figure A15


png("Figures/Figure A15.png", width = 2500, height = 3000, res=300)
ggplot(rbind(education_data_acmes.vote_choice, education_data_mm.vote_choice))+
  geom_point(aes(x=estimate, y=labels_score2, color = Education))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = Education, y=labels_score2), height = 0.2)+
  scale_y_continuous(breaks = c(1:30), labels = unique(as.character(education_data_acmes.vote_choice$labels))[c(19:30,1:18)],
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue", "grey"))+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", 
        panel.border = element_rect(colour = "black", fill=NA, linewidth=.5), 
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off()




## Plot Figure A16

png("Figures/Figure A16.png", width = 2500, height = 3000, res=300)
ggplot(rbind(education_data_acmes.better_pol, education_data_mm.better_pol))+
  geom_point(aes(x=estimate, y=labels_score2, color = Education))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = Education, y=labels_score2), height = 0.2)+
  scale_y_continuous(breaks = c(1:30), labels = unique(as.character(education_data_acmes.vote_choice$labels))[c(19:30,1:18)],
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue", "grey"))+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", 
        panel.border = element_rect(colour = "black", fill=NA, linewidth=.5), 
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off()


## Plot Figure A17

png("Figures/Figure A17.png", width = 2500, height = 3000, res=300)
ggplot(rbind(education_data_acmes.prob_vote, education_data_mm.prob_vote))+
  geom_point(aes(x=estimate, y=labels_score2, color = Education))+
  geom_errorbarh(aes(xmin = lower, xmax=upper, color = Education, y=labels_score2), height = 0.2)+
  scale_y_continuous(breaks = c(1:30), labels = unique(as.character(education_data_acmes.vote_choice$labels))[c(19:30,1:18)],
                     trans = "reverse")+
  scale_color_manual(values = c("red", "blue", "grey"))+
  facet_wrap(~type,
             labeller = labeller(
               type = c(`Marginal Means` = "Marginal Means", `Acmes` = "Average Marginal Component Effect")
             ), scales = 'free_x')+
  ylab("")+
  theme_minimal()+
  theme(legend.position = "bottom", 
        panel.border = element_rect(colour = "black", fill=NA, linewidth=.5), 
        axis.text.y = element_text(face = c("bold", rep("plain", 2), 
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 5),
                                            "bold", rep("plain", 2),
                                            "bold", rep("plain", 2))))
dev.off()





